package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.entity.Community;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int insertCommunity(Community community) {
        String sql = "insert into community values(null,?,?,?,?,?,?,?,now(),?,?,?,?,?,?)";
        Object object[] = {
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityShopCount(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityStatus(),
                community.getCommunityUserCount(),
                community.getDistance()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int updateCommunity(Community community) {
//        String sql = "insert into community values(null,?,?,?,?,?,?,?,now(),?,?,?,?,?,?)";
        String sql = "update community " +
                "set communityName = ?,communityProvince=?,communityCity=?," +
                "communityDistrict=?,communityAddress=?," +
                "communityLatitude=?,communityLongitude=?," +
                "communityAreaCode=?,communityCoordinates=?" +
                "where communityId=?";
        Object[] object = {community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityId()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int deleteCommunity(Integer communityId) {
        String sql="delete from community where communityId=?";
        Object []objects={communityId};
        int row=JDBCDruidUtil.update(sql,objects);

        return row;
    }

    @Override
    public List<Community> selectCommunitys(Map<String, String> map) {
        List<Community> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuilder sb = new StringBuilder("SELECT*from community where 1=1 ");
        List param = new ArrayList();
        //判断communityName是否有数据，如果有，则拼接
        String communityName = map.get("communityName");
//        System.out.println("111"+communityName);
        if (communityName != null && !"".equals(communityName)) {//axios的发送的路径参数没有时为null
            sb.append(" and communityName LIKE ? ");
            param.add("%" + communityName + "%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (!"".equals(startTime) && startTime != null) {//axios的发送的路径参数没有时为null
            sb.append(" and createTime BETWEEN ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }

        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" order by communityId desc limit ?,? ");
        param.add((pageNum - 1) * pageSize);
        param.add(pageSize);


        String sql = sb.toString();
        System.out.println(sql);

        Object[] object = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()) {
                Community community = new Community(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getDate(9),
                        resultSet.getInt(10),
                        resultSet.getInt(11),
                        resultSet.getString(12),
                        resultSet.getInt(13),
                        resultSet.getInt(14),
                        resultSet.getString(15)
                );

                list.add(community);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectCommunityCount(Map<String, String> map) {
        int count = 0;
        //动态sql和动态数组
        StringBuilder sb = new StringBuilder("SELECT count(1) from community where 1=1 ");
        List param = new ArrayList();
        //判断name是否有数据，如果有，则拼接
        String communityName = map.get("communityName");
        if (!"".equals(communityName) && communityName != null) {//axios的发送的路径参数没有时为null
            sb.append(" and communityName LIKE ? ");
            param.add("%" + communityName + "%");
        }
        //判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (!"".equals(startTime) && startTime != null) {//axios的发送的路径参数没有时为null
            sb.append(" and createTime BETWEEN ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }

        String sql = sb.toString();
        //System.out.println(sql);

        Object[] object = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtil.close();
        }

        return count;
    }
}
